package com.heer.gymnasium.util;

import android.app.Activity;
import android.util.DisplayMetrics;

import java.lang.reflect.Field;

/**
 * ScreenUtils Created by hanj on 14-9-25.
 */
public class ScreenUtils {
	private static int screenW;
	private static int screenH;
	private static float screenDensity;
	private static int statusBarHeight;

	public static void initScreen(Activity mActivity) {
		DisplayMetrics metric = new DisplayMetrics();
		mActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
		screenW = metric.widthPixels;
		screenH = metric.heightPixels;
		screenDensity = metric.density;
		Class<?> c = null;
		Object obj = null;
		Field field = null;
		int x = 0;
		try {
			c = Class.forName("com.android.internal.R$dimen");
			obj = c.newInstance();
			field = c.getField("status_bar_height");
			x = Integer.parseInt(field.get(obj).toString());
			statusBarHeight = mActivity.getResources().getDimensionPixelSize(x);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public static int getStatusBarHeight() {
		return statusBarHeight;
	}

	public static int getScreenW() {
		return screenW;
	}

	public static int getScreenH() {
		return screenH;
	}

	public static float getScreenDensity() {
		return screenDensity;
	}

	/** 根据手机的分辨率从 dp 的单位 转成为 px(像素) */
	public static int dp2px(float dpValue) {
		return (int) (dpValue * getScreenDensity() + 0.5f);
	}

	/** 根据手机的分辨率从 px(像素) 的单位 转成为 dp */
	public static int px2dp(float pxValue) {
		return (int) (pxValue / getScreenDensity() + 0.5f);
	}

}
